import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig(({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd())

  return {
    plugins: [react()],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src')
      }
    },
    server: {
      open: true,
      port: 9018,
      host: true, // 允许局域网访问
      proxy: {
        '/wx': {
          target: env.VITE_API_BASE_URL, // 使用加载的环境变量
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/wx/, '')
        },
        '/deepseek': {
          target: env.VITE_API_DEEPSEEK_URL, // 代理的目标地址
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/deepseek/, '')
        }
      }
    }
  }
})
